<?php
namespace Home\Controller;
use Home\Common\Common;


class SalaryController extends Common{
//    薪资查看
    public function index(){
        $salary = M('salary');
        if(empty($_GET['search'])){
            $where = "";
        }else{
            $where = '`event` like "%'.$_GET['value'].'%" or `entry_preson` like "%'.$_GET['value'].'%"';
        }
        $count=$salary->where(array('start=1'),$where)->count();
        $page=new \Think\Page($count,5);//实例化Think里面的Page方法，并设置每页显示的数目
        $show=$page->show();
        $cont = $salary->where('start=1',$where)->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('show',$show);
        $this->assign('cont',$cont);
        $this->display();
    }
//    薪资详情
    public function edit(){
        if(empty($_GET['id'])){
            $this->error('参数错误，请重新尝试',U('index'));
        }else {
            $salary = M('salary');
            $staffinfo = M('staffinfo');
            $staffinfo = $staffinfo->select();
            $postinfo = M('postinfo');

            $salary = $salary->where(array('lid'=>$_GET['id']))->find();
            $postinfo = $postinfo->getField('pt_id,wages,subsidy');//用pt_id为索引，查询工资、岗位补贴
            $this->assign('staffinfo', $staffinfo);
            $this->assign('postinfo', $postinfo);
            $this->assign('salary',$salary);
            $this->display();
        }
    }
//    薪资回收站
    public function  index_hs(){
        $salary = M('salary');
        if(empty($_GET['search'])){
            $where = "";
        }else{
            $where = '`event` like "%'.$_GET['value'].'%" or `entry_preson` like "%'.$_GET['value'].'%"';
        }
        $count=$salary->where(array('start=0'),$where)->count();
        $page=new \Think\Page($count,5);//实例化Think里面的Page方法，并设置每页显示的数目
        $show=$page->show();
        $cont = $salary->where('start=0',$where)->limit($page->firstRow.','.$page->listRows)->select();
        if(empty($salary)){
            $this->display();
        }
        $this->assign('salary',$salary);
        $this->assign('show',$show);
        $this->assign('cont',$cont);
        $this->display();
    }
//    薪资添加
    public function add(){
        if(empty($_POST['hidden'])){
            $staffinfo = M('staffinfo');
            $staffinfo = $staffinfo->select();
            $postinfo=M('postinfo');
            $postinfo=$postinfo->getField('pt_id,wages,subsidy');//用pt_id为索引，查询工资、岗位补贴
            $this->assign('staffinfo',$staffinfo);
            $this->assign('postinfo',$postinfo);

            $this->display();
        }else {
            $salary = M('salary');
            $staffinfo = M('staffinfo');//实例化员工表
            $postinfo=M('postinfo');//实例化岗位表
            $staffinfo->where(array('stf_id'=>$_POST['id']))->setField('rewards',$_POST['rewards'],$_POST['cf'],$_POST['rewards']);
          	$data['event']=$_POST['event'];
            $data['explain']=$_POST['explain'];
            $data['entry_person']=$_POST['entry_person'];
            $data['entry_time']=$_POST['entry_time'];
            $sta = $staffinfo->where(array('std_id'=>$_SESSION['id']))->select();
            $postinfo=$postinfo->getField('pt_id,wages,subsidy');//用pt_id为索引，查询工资、岗位补贴
            $salary->data($data)->add();
            $staffinfo = $staffinfo->select();
            $this->error('添加成功',U('index'));
            $this->assign('staffinfo', $staffinfo);
            $this->assign('postinfo', $postinfo);
            $this->assign('sta',$sta);
            $this->display();
        }
    }
//    放入回收站
    public function del(){
        if(empty($_GET['id'])){
            $this->error("参数错误，请重新尝试",U('index'));
        }
        $salary = M('salary');
        $data['start'] = '0';
        $sa = $salary->where(array('lid'=>$_GET['id']))->save($data);
        if($sa){
            $this->success('删除成功，请在回收站查看');
        }else {


            $this->error('删除失败');
        }
    }
//    回收站彻底删除
    public function  del_cd(){
        if(empty($_GET['id'])){
            $this->error('参数错误');
        }else {
            $cont = M('salary');
            $cont = $cont->where(array('lid'=>$_GET['id']))->delete();
            if($cont){
                $this->success('删除成功');
            }else{
                $this->success('删除失败，请重新尝试');
            }
        }
    }
    //    回收站数据恢复
    public function del_hs(){
        if(empty($_GET['id'])){
            $this->error("参数错误，请重新尝试",U('index'));
        }
        $salary = M('salary');
        $data['start'] = '1';
        $con = $salary->where(array('lid'=>$_GET['id']))->save($data);
        if($con){
            $this->success('数据恢复成功');
        }else {


            $this->error('数据恢复失败');
        }
    }
}
